package sdk.webview.fmc.com.fmcsdk.check.adapter;

import static sdk.webview.fmc.com.fmcsdk.util.ImageUtils.getDisplayMetrics;

import android.content.Context;
import android.graphics.Color;
import android.preference.PreferenceManager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import sdk.webview.fmc.com.fmcsdk.R;
import sdk.webview.fmc.com.fmcsdk.app.FMCApplication;
import sdk.webview.fmc.com.fmcsdk.bean.DDTypeBean;
import sdk.webview.fmc.com.fmcsdk.check.bean.GetDetailsBean;
import sdk.webview.fmc.com.fmcsdk.check.bean.ImageSelectBean;
import sdk.webview.fmc.com.fmcsdk.util.Constant;
import sdk.webview.fmc.com.fmcsdk.util.GsonUtil;
import sdk.webview.fmc.com.fmcsdk.util.KLog;
import sdk.webview.fmc.com.fmcsdk.util.StringUtils;

public class ReadonlyWorkDetailAdapter extends BaseQuickAdapter<GetDetailsBean.RecordDTO.PointsDTO.CheckListDTO, BaseViewHolder> {
    private OnPhotoVideoShowListener photoVideoClick;
    private  ImageSelectAdapter imageSelectAdapter;
    public ReadonlyWorkDetailAdapter(int layoutResId, OnPhotoVideoShowListener photoVideoClick) {
        super(layoutResId);
        this.photoVideoClick = photoVideoClick;
    }

    @Override
    public long getItemId(int position) {
        return Long.parseLong(getData().get(position).getId());
    }

    @Override
    protected void convert(BaseViewHolder holder, GetDetailsBean.RecordDTO.PointsDTO.CheckListDTO item) {
        if (item.getHasMeter() == 1) {
            //是设备水表
            holder.setGone(R.id.ll_shui, true)
                    .setGone(R.id.ll_item, false);

            if (item.getChecked() == 1) {  //已检测
                holder.setImageResource(R.id.iv_right_three, R.drawable.icon_check);
                holder.setGone(R.id.iv_right_three,true);
            } else {
                holder.setGone(R.id.iv_right_three,false);
            }

            TextView tvDetectionValue = holder.getView(R.id.tv_detection_value);
            holder.setText(R.id.tv_water_meter, (holder.getAdapterPosition() + 1) + "." + item.getMeterName());
            tvDetectionValue.setText(StringUtils.isEmpty(item.getMeterRead()) ? "--" : item.getMeterRead());
            holder.addOnClickListener(R.id.rl_enter);
            holder.addOnClickListener(R.id.ll_shui);
        }else {
            holder.setGone(R.id.ll_shui, false)
                    .setGone(R.id.ll_item, true);

            holder.setText(R.id.tv_title, (holder.getAdapterPosition() + 1) + "." + item.getTitle());

            TextView tv_standard = holder.getView(R.id.tv_standard);
            tv_standard.setText(item.getStandard());

            if (item.getChecked() == 1) {
                holder.setGone(R.id.iv_status, true);
                holder.setImageResource(R.id.iv_status, R.drawable.icon_check);

            } else {
                holder.setGone(R.id.iv_status, false);
            }

            String checkResultType = item.getCheckResultType();
            List<DDTypeBean.RecordsBean> recordsBeans = FMCApplication.typeMap.get(checkResultType);
            if (null != recordsBeans) {
                LinearLayout llStateContent = holder.getView(R.id.ll_state_content);
                createStateUi(recordsBeans, llStateContent, item, holder);
            }

            holder.setGone(R.id.ll_remake, !StringUtils.isEmpty(item.getRemark()));
            holder.setText(R.id.tv_remake, item.getRemark());
            holder.setText(R.id.tv_remake_font_count, item.getRemark().length()+"/100");

            holder.setGone(R.id.ll_camera, !item.getPictureUrls().isEmpty());
            RecyclerView rv_img = holder.getView(R.id.rv_img);
            rv_img.setLayoutManager(new GridLayoutManager(mContext, 5));


           // List<ImageSelectBean> imageList = getImageSelectBeans(item,mContext);
//            item.setImageList(imageList);



            imageSelectAdapter = new ImageSelectAdapter(item.getImageListShowNoUpload());
            rv_img.setAdapter(imageSelectAdapter);
            imageSelectAdapter.setOnItemChildClickListener((adapter, view, position) -> {
                if (view.getId() == R.id.iv_view) {
                    photoVideoClick.photoVideoClick(position, adapter.getData());
                }
            });
            holder.setText(R.id.tv_num,"("+imageSelectAdapter.getSelectImgNum()+")");

        }


    }

    private  List<ImageSelectBean> getImageSelectBeans(GetDetailsBean.RecordDTO.PointsDTO.CheckListDTO item, Context context) {
        List<ImageSelectBean> imageList = item.getImageListShow();
        if (!item.getPictureUrls().isEmpty()){
            for (GetDetailsBean.RecordDTO.PointsDTO.CheckListDTO.PictureUrlsDTO pictureUrl : item.getPictureUrls()) {
                ImageSelectBean imageSelectBean;
                if (pictureUrl.getUrl().contains("mp4")){
                    if (!pictureUrl.getUrl().contains("http")){
                        String baseUrl = PreferenceManager.getDefaultSharedPreferences(context).getString(Constant.BASE_ADDRESS_URL, "");
                        imageSelectBean = new ImageSelectBean(baseUrl+pictureUrl.getUrl(),2);
                        imageList.add(imageSelectBean);
                    }


                }else {
                    if (!pictureUrl.getUrl().contains("http")){
                        String baseUrl = PreferenceManager.getDefaultSharedPreferences(context).getString(Constant.BASE_ADDRESS_URL, "");
                        imageSelectBean = new ImageSelectBean(baseUrl+pictureUrl.getUrl(),1);
                        imageList.add(imageSelectBean);
                    }

                }
            }
        }else {
            imageList.clear();
        }
        return imageList;
    }


    private void createStateUi(List<DDTypeBean.RecordsBean> recordsBeans, LinearLayout llStateContent, GetDetailsBean.RecordDTO.PointsDTO.CheckListDTO item, BaseViewHolder holder) {
        llStateContent.removeAllViews();
        for (int i = 0; i < recordsBeans.size(); i++) {
            LinearLayout rowLayout = new LinearLayout(mContext);
            // 设置 LinearLayout 的布局参数（宽度为自适应内容，高度为自适应内容）
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,// 宽度沾满
                    LinearLayout.LayoutParams.WRAP_CONTENT   // 高度自适应内容
            );
            rowLayout.setLayoutParams(layoutParams);

            // 设置 LinearLayout 的方向（可以选择水平或垂直）
            rowLayout.setOrientation(LinearLayout.HORIZONTAL);


            // 每行最多放三个元素
            for (int j = 0; j < 3; j++) {
                // 计算当前显示的数据项在 recordsBeans 中的索引
                int currentIndex = i * 3 + j;
                if (currentIndex >= recordsBeans.size()) break; // 如果索引超过集合大小，停止添加

                // 创建每条数据的 LinearLayout
                LinearLayout itemLayout = new LinearLayout(mContext);
                itemLayout.setOrientation(LinearLayout.HORIZONTAL);  // 设置垂直方向排列
                itemLayout.setLayoutParams(new LinearLayout.LayoutParams(
                        0, // 宽度为 0
                        LinearLayout.LayoutParams.WRAP_CONTENT,  // 高度自适应
                        1 // 权重为 1，等比例分配空间
                ));

                // 创建 ImageView 和 TextView
                ImageView imageView = new ImageView(mContext);
                TextView textView = new TextView(mContext);

                // 设置 ImageView 和 TextView 的 UI 样式
                if (null != item.getCheckResult() && item.getCheckResult().equals(recordsBeans.get(currentIndex).getCode())) {
                    imageView.setImageResource(R.mipmap.icon_select);
                } else {
                    imageView.setImageResource(R.mipmap.icon_circle_nor_enable);
                }

                textView.setText(recordsBeans.get(currentIndex).getDescription()); // 使用 recordsBeans 中的文本数据
                textView.setTextColor(Color.parseColor("#393A42"));  // 设置字体颜色为 #393A42
                textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18); // 设置字体大小为 18sp

                // 设置 LayoutParams（宽度为 0，以权重分配空间）
                LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT, // 宽度为 MATCH_PARENT
                        LinearLayout.LayoutParams.WRAP_CONTENT  // 高度自适应
                );
                LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT,  // 宽度为 MATCH_PARENT
                        LinearLayout.LayoutParams.WRAP_CONTENT  // 高度自适应
                );

                int leftMargin = (int) (7 * getDisplayMetrics(mContext).density);
                textParams.setMargins(leftMargin, 0, 0, 0);

                imageView.setLayoutParams(imageParams);
                textView.setLayoutParams(textParams);

                // 将 ImageView 和 TextView 添加到每条数据的 LinearLayout
                itemLayout.addView(imageView);
                itemLayout.addView(textView);
                itemLayout.setGravity(Gravity.CENTER_VERTICAL);


                // 为 itemLayout 设置边距
                if (j == 0) {
                    // 最左边的元素
                    //itemLayout.setPadding(0, 0, 0, 0);  // 设置左边距为 7dp
                    itemLayout.setHorizontalGravity(Gravity.START);
                } else if (j == 1) {
                    // 中间的元素
                    //itemLayout.setPadding(15, 0, 15, 0); // 设置左右边距为 15dp
                    if (j == recordsBeans.size() - 1) {
                        itemLayout.setHorizontalGravity(Gravity.END);
                    } else {
                        itemLayout.setHorizontalGravity(Gravity.CENTER_HORIZONTAL);
                    }

                } else {
                    // 最右边的元素
                    //itemLayout.setPadding(0, 0, 0, 0);  // 设置右边距为 7dp
                    itemLayout.setHorizontalGravity(Gravity.END);
                }


                // 将 itemLayout 添加到当前行的 rowLayout
                rowLayout.addView(itemLayout);


            }

            llStateContent.addView(rowLayout);


        }
    }

    public interface OnPhotoVideoShowListener{
        void photoVideoClick(int position, List<ImageSelectBean> imgList);
    }
}
